<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/course_php.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
	$con = @mysql_connect('localhost', 'root', '');
	if (!$con) {
		die("Đã xảy ra lỗi trong quá trình xử lý." . mysql_error());
	}
	
	mysql_set_charset('utf8');
	$db_selected = @mysql_select_db('studial');
	if (!$db_selected) {
		die("Đã xảy ra lỗi trong quá trình xử lý." . mysql_error());
	}

	$sql="SELECT * FROM `courses` WHERE `administrator_id`='".$_COOKIE["studialuserid"]."' AND `id`='".$_GET["id"]."'";
	$result = mysql_query($sql);
	$rows = mysql_num_rows($result);
	$i = 0;
	if ($rows > 0) {
		$i = 3; //is teacher
	}
	

	$sql="SELECT * FROM `enroll` WHERE `user_id`='".$_COOKIE["studialuserid"]."' AND `course_id`='".$_GET["id"]."'";
	$result = mysql_query($sql);
	$rows = mysql_num_rows($result);
	if ($rows > 0) {
		$arr = mysql_fetch_array($result);
		if($arr['role'] == 0) {
			$i = 1; //student
		}
		else {
			$i = 2; //TA
		}
	}
	
	//Get homeboard info
	$sql="SELECT * FROM `courses` WHERE `id`='".$_GET['id']."'";
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
	$coursename=$row["name"];
	$description=$row["description"];
	
	//Get homeboard info
	$sql="SELECT * FROM `users` WHERE `id`='".$_COOKIE['studialuserid']."'";
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
	$fullnameuser=$row["lastname"]." ".$row["firstname"];
?>
<title><?php echo $coursename; ?></title>
<link rel="stylesheet" type="text/css" href="../css/global.css" />
<link rel="stylesheet" type="text/css" href="../css/loggedpages.css" />
<!-- InstanceBeginEditable name="head" -->
<script language="javascript" type="text/javascript" src="../javascript/event-form.js">
</script>
<!-- InstanceEndEditable -->
</head>

<body>
	<div class="header">
        <div class="innercontent">
            <a href="<?php echo "../users/homeboard.php?id=".$_COOKIE["studialuserid"]; ?>"><img alt="Studial" class="logo" src="../images/logo.jpg" /></a>
            <form id="searchform" method="get" action="../search/results.php">
                <div id="searchblock">
                    <input id="searchfield" name="searchfield" type="text" placeholder="Tìm kiếm" />
                    <button id="searchbutton" type="submit" name="search" value="1" title="Tìm kiếm"><img id="searchicon" src="../images/searchicon.png" /></button>
                </div>           
            </form>
            <div id="rightheader">
                <a class="headerbutton" id="logout" href="../unlogged/logout.php"><span id="borderleft"></span>Đăng xuất</a>
                <a class="headerbutton" id="toboard" href="<?php echo "../users/homeboard.php?id=".$_COOKIE["studialuserid"]; ?>"><?php echo $fullnameuser; ?></a>
            </div>
        </div>
    </div>
    <div class="innercontent">
        <div id="leftcolumn">
        	<div id="sidenavcontainer">	
                <ul id="sidenav">
            		<li><a class="navlink" href="homeboard.php?id=<?php echo $_GET['id']; ?>"><span id="boardnav">Bảng</span></a></li>
                    <li><a class="navlink" href="info.php?id=<?php echo $_GET['id']; ?>"><span id="infonav">Thông tin</span></a></li>
                    <li><a class="navlink" href="eventlist.php?id=<?php echo $_GET['id']; ?>"><span id="eventnav">Sự kiện</span></a></li>
                    <li><a class="navlink" href="memberlist.php?id=<?php echo $_GET['id']; ?>"><span id="membernav">Thành viên</span></a></li>
					<?php if ($i == 3 || $i == 2) echo "<li><a class=\"navlink\" href=\"\"><span id=\"gradenav\">Bảng điểm</span></a></li>"; ?>
            	</ul>
            </div>
        </div>
        <div id="contentcolumn">
        	<div id="contentcolheader">
            	<h2><?php echo $coursename; ?></h2>
                <h3> <!-- InstanceBeginEditable name="pagename" --><!-- InstanceEndEditable --></h3>
				<?php 
					if ($i == 0) { 
						echo "<a id=\"attendcourse\" href=\"../users/attendcourse.php?id=".$_GET["id"]."\"><button class=\"greenbutton\">Tham gia</button></a>";
					}
				 	else if ($i == 1 || $i == 2) {
						echo "<a id=\"leavecourse\" href=\"../users/leavecourse.php?id=".$_GET["id"]."\"><button class=\"greenbutton\">Rời bỏ</button></a>"; 
					}
					else if ($i == 3) {
						echo "<a id=\"removecourse\" href=\"../users/removecourse.php?id=".$_GET["id"]."\"><button class=\"greenbutton\">Kết thúc</button></a>"; 
					}
				?>  
            </div>
		<!-- InstanceBeginEditable name="maincontent" -->
        <?php
			
			function getrole($id)
			{
				$result = mysql_query("select * from `works` where `user_id` = '".$id."';");
				$rows = mysql_fetch_array($result);
				return $rows['role'];
					// 1 -> sinh vien
					// 2 -> nghien cuu sinh
					// 3 -> tro giang
					// 4 -> giang vien	
			}
		
			function iscourseteacher($id)
			{
				$result = mysql_query("select * from `courses` where `administrator_id` = '".$id."';");
				$rows_num = mysql_num_rows($result);
				return $rows_num;
			}
			
			function geteventinfo($id)
			{
				$result = mysql_query("select * from `assignments` where `id` = '".$_GET['id']."';");
				$rows = mysql_fetch_array($result);
				return $rows;	
			}
			//---------------------------------------
			$user_id = $_COOKIE['studialuserid'];
			$eventinfo = geteventinfo($_GET['id']);
			
			if(iscourseteacher($user_id) != 0 || getrole($user_id) == 4)
			{			
			
				if(isset($_POST['eventname']))
				{
					$eventname = $_POST['eventname'];
					$eventdescription = $_POST['eventdescription'];
					$startdate = $_POST['day1']."-".$_POST['month1']."-".$_POST['year1'];
					$finishdate = $_POST['day2']."-".$_POST['month2']."-".$_POST['year2'];
					
					mysql_query("update `assignments` set `name` = '".$eventname."', `description` = '".$eventdescription."', `startdate` = '".$startdate."', `finishdate` = '".$finishdate."' where `id` = '".$_GET['id']."';");
					header ("location: eventlist.php?id=".$_GET['id']);
				}
		
		?>
	<h2 align="center">Thay đổi sự kiện</h2>
    	<form id="createCourse" action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $_GET['id']; ?>" method="post">
            <table>
            	<tr>
                	<td>Tên sự kiện</td>
                    <td><input type="text" name="eventname" value="<?php echo $eventinfo['name']; ?>" size="40" /></td>
                </tr>
            	<tr>
                	<td>Mô tả<br/>yêu cầu đề bài</td>
                    <td><textarea name="eventdescription" rows="5" cols="60"><?php echo $eventinfo['description']; ?></textarea></td>
                </tr>
                <tr>
                	<td>Ngày bắt đầu</td>
                    <td align="left">
                    <?php 
						$statedate = $eventinfo['startdate']; 
						list($year, $month, $day) = explode("-",$statedate);					
					?>
                        <select class="dropinput" name="day1" id="day1" onfocus="setDays1();">
                            <option value="0" selected="selected"><?php echo $day; ?></option>
                        </select>
                        <select class="dropinput" name="month1" id="month1" onfocus="setMonths1();" onchange="setDays1();">
                            <option value="0" selected="selected"><?php echo $month; ?></option>
                        </select>
                        <select class="dropinput" name="year1" id="year1" onfocus="setYears1();" onchange="setDays1(); setMonths1();">
                            <option value="0" selected="selected"><?php echo $year; ?></option>
                        </select>
                	</td>
                </tr>
                <tr>
                	<td>Ngày kết thúc</td>
                    <td align="left">
                    <?php 
						$finishdate = $eventinfo['finishdate']; 
						list($year, $month, $day) = explode("-",$finishdate);					
					?>
                        <select class="dropinput" name="day2" id="day2" onfocus="setDays2();">
                            <option value="0" selected="selected"><?php echo $day; ?></option>
                        </select>
                        <select class="dropinput" name="month2" id="month2" onfocus="setMonths2();" onchange="setDays2();">
                            <option value="0" selected="selected"><?php echo $month; ?></option>
                        </select>
                        <select class="dropinput" name="year2" id="year2" onfocus="setYears2();" onchange="setDays2(); setMonths2();">
                            <option value="0" selected="selected"><?php echo $year; ?></option>
                        </select>
                	</td>
                </tr>
                <tr align="center">
                    <td colspan="2"><input type="submit" class="greenbutton" value="Sửa" /></td>
                </tr>
        	</table>
        </form>
        <?php
			}
			else
			{
				echo "Bạn không có quyền truy cập vào trang này";
				exit;
			}
		
		?>
<!-- InstanceEndEditable -->
    	</div>
    </div>
    <div class="footer">
    	<div id="leftfooter">Studial @ 2012 - Nhóm 01</div>
        <div id="rightfooter">
			<a href="../subpages/createschool.php?id=<?php echo $_COOKIE['studialuserid']; ?>" title="Đăng ký thêm tên trường vào hệ thống">Đăng ký trường</a>
            -
            <a href="../subpages/createfaculty.php?id=<?php echo $_COOKIE['studialuserid']; ?>" title="Đăng ký thêm tên ngành học vào hệ thống">Đăng ký ngành</a>
        </div>
    </div>      
</body>
<!-- InstanceEnd --></html>
